package zouran.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import zouran.entity.News;
import zouran.mapper.NewsMapper;
import zouran.service.NewsService;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: zouran
 * @Description:
 * @Date: Created in 2022 2022/12/3 16:30
 **/
@Service
public class NewsServiceImpl implements NewsService {
    private NewsMapper newsMapper;
    @Autowired
    public void setNewsMapper(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }
    public List<News> select_byTitle(String title){
    return newsMapper.select_byTitle(title);
}
    public List<News> select_byAuthor(String author)
    {
        QueryWrapper<News> wp=new QueryWrapper<>();
        return newsMapper.selectList(wp.eq("newsAuthor",author));
    }
    public List<News> getAllNews(){
        return newsMapper.selectList(null);
    }
    public boolean delete_news(News news){
       return newsMapper.deleteById(news)==1;
    }
    public boolean add_news(News news){
        return newsMapper.insert(news) == 1;
    }
    public boolean update_news(News news){return newsMapper.updateById(news)==1;}
    public List<News> select_byWrapper(News news){
        List<News> list;
        QueryWrapper<News> wrapper=new QueryWrapper<>();
        if(!Objects.equals(news.getNewsAuthor(), ""))
        {
            wrapper.like("newsAuthor",news.getNewsAuthor());
        }
        if(!Objects.equals(news.getNewsTitle(), ""))
        {
            wrapper.like("newsTitle",news.getNewsTitle());
        }
        if(!Objects.equals(news.getNewsSort(), ""))
        {
            wrapper.like("newsSort",news.getNewsSort());
        }
        if(!Objects.equals(news.getNewsDesc(), ""))
        {
            wrapper.like("newsDesc",news.getNewsDesc());
        }
        if(!Objects.equals(news.getNewsContent(), "")){
            wrapper.like("newsContent",news.getNewsContent());
        }
        list=newsMapper.selectList(wrapper);
        return list;
    }
    public Map<String,Object> upload_file(MultipartFile file){
        String prefix;
        String dateStr;
        //保存上传
        if(file!=null){
            String originalName = file.getOriginalFilename();
            assert originalName != null;
            prefix=originalName.substring(originalName.lastIndexOf(".")+1);
            Date date = new Date();
            String uuid = UUID.randomUUID()+"";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateStr = simpleDateFormat.format(date);
//            String filepath = "D:\\软件\\IntelliJ IDEA\\project\\springboot\\WEBMIS\\" +
//                    "src\\main\\resources\\static\\images\\upload\\" + dateStr+"\\"+uuid+"." + prefix; //本地存储路径
//           String filepath = "/home/webmis/upload/"+dateStr+"/" +uuid+"." + prefix; //centos7.6系统云服务器存储路径
            String filepath = "D:\\upload\\"+dateStr+"/" +uuid+"." + prefix; //centos7.6系统云服务器存储路径
            //"/home/webmis/upload/"路径和yml配置文件的 "static-locations:"中的"file:"后面路径一致,注意"/"结尾
            File files=new File(filepath);
            if(!files.getParentFile().exists()){
                files.getParentFile().mkdirs();//当文件不存在时，自动生成
            }
            try {
                file.transferTo(files);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Map<String,Object> map2=new HashMap<>();
            Map<String,Object> map=new HashMap<>();
            map.put("code",0);
            map.put("msg","");
            map.put("data",map2);
            map2.put("src","/static/"+dateStr+"/"+uuid+"." + prefix);
            return map;
        }
        else {
            Map<String, Object> map = new HashMap<>();
            map.put("code", 1);
            map.put("msg", "");
            return map;
        }
    }


}

